package gz.metro.datasync.controller;


import gz.metro.datasync.entity.form.SyncLogForm;
import gz.metro.datasync.service.DatasyncService;
import gz.metro.datasync.utils.ApiResponse;
import gz.metro.datasync.utils.Result;
import gz.metro.datasync.utils.page.PageQueryRequest;
import gz.metro.datasync.utils.page.PagerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenwx
 * @description 数据同步服务控制类
 * @createTime 2019-11-25 11:05
 */
@Slf4j
@RestController
@RequestMapping("/datasync")
@Api(tags = "组织同步", value = "DatasyncController")
public class DatasyncController  {

    @Autowired
    private DatasyncService dataSyncService;

    /**
     * 执行组织全量同步
     * @return 同步结果
     */
    @GetMapping("execOrgFullSync")
    @ApiOperation("执行组织全量同步")
    public ApiResponse execOrgFullSync(){
        log.info("execOrgFullSync");

        Result result = dataSyncService.execOrgFullSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 执行组织增量同步
     * @return 同步结果
     */
    @ApiOperation("执行组织增量同步")
    @GetMapping("execOrgIncSync")
    public ApiResponse execOrgIncSync(){
        log.info("execOrgIncSync");

        Result result = dataSyncService.execOrgIncSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 执行用户全量同步
     * @return 同步结果
     */
    @ApiOperation("执行用户全量同步")
    @GetMapping("execUserFullSync")
    public ApiResponse execUserFullSync(){
        log.info("execUserFullSync");

        Result result = dataSyncService.execUserFullSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 执行用户增量同步
     * @return 同步结果
     */
    @ApiOperation("执行用户增量同步")
    @GetMapping("execUserIncSync")
    public ApiResponse execUserIncSync(){
        log.info("execUserIncSync");

        Result result = dataSyncService.execUserIncSync();
        if(result.isSuccess()){
            return ApiResponse.ok(result.getMessage());
        }else{
            return ApiResponse.badRequest(result.getMessage());
        }
    }

    /**
     * 分页查询同步日志
     * @return 分页查询结果
     */
    @ApiOperation("分页查询同步日志")
    @GetMapping("pageQuerySyncLog")
    public Object pageQuerySyncLog(
            @ApiParam("分页查询参数") PageQueryRequest pageQueryRequest){
        log.info("pageQuerySyncLog");
        log.info("pageQueryRequest", pageQueryRequest);
        Result<PagerVO<SyncLogForm>> resultEntity = dataSyncService.pageQuerySyncLog(pageQueryRequest);
        if(resultEntity.isSuccess()){
            return resultEntity.getBusinessObject();
        }else{
            return ApiResponse.badRequest(resultEntity.getMessage());
        }
    }

}
